﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Linq;
public class Task : MonoBehaviour
{
    public string id;
    public string caption;
    public enum State
    {
        get, progress, finish, reward
    }
    public State state = State.get;

    public delegate void getDelegate(string info);
    public event getDelegate getEvent;

    public delegate void checkDekegate(string id, int amount);
    public event checkDekegate checkEvent;

    public delegate void finishDelegate(string info);
    public event finishDelegate finishevent;

    public delegate void rewarsDelegate(string info);
    public event rewarsDelegate rewardEvent;

    public List<TaskCondition> taskcondition = new List<TaskCondition>();
    public List<TaskReward> taskRewards = new List<TaskReward>();

    public Task(string id)
    {
        this.id = id;
        XElement xe = TaskManager.instance.xele.Element("task"+id);
        IEnumerable<XElement> a = xe.Elements("conditionID");
        IEnumerator<XElement> b = 
            xe.Elements("conditionTargetAmount").GetEnumerator();
        IEnumerable<XElement> c = xe.Elements("rewardID");
        IEnumerator<XElement> d =
            xe.Elements("rewardAmount").GetEnumerator();
        foreach (var s in a)
        {
            b.MoveNext();
            TaskCondition tc = 
                new TaskCondition(s.Value,0,int.Parse(b.Current.Value),false);
            taskcondition.Add(tc);
            checkEvent += tc.Check;
        }
        foreach (var s in c)
        {
            d.MoveNext();
            TaskReward tr = new TaskReward(s.Value, int.Parse(d.Current.Value));
            taskRewards.Add(tr);
        }
        getEvent += Notification.instance.PrintInfo;
        finishevent += Notification.instance.PrintInfo;
        rewardEvent += Notification.instance.PrintInfo;
        Get();

    }
    public void Get()
    {
        getEvent("接受任务task" + this.id);
        state = State.progress;
    }
    public void Check(string id, int amount)
    {
        checkEvent(id, amount);
        for (int i = 0; i < taskcondition.Count; i++)
        {
            if (!taskcondition[i].isFinish)
            {
                state = State.progress;
                return;
            }
        }
        state = State.finish;
        Finish();
    }

    //完成任务
    public void Finish()
    {
        finishevent("任务" + this.id + "完成！！");
        state = State.reward;
    }

    //获取奖励
    public void Reward()
    {
        if (state != State.reward) return;
        TaskReward tr;
        for (int i = 0; i < taskRewards.Count; i++)
        {
            tr = taskRewards[i];
            rewardEvent("奖励物品" + tr.id + "奖励数量" + tr.amount);
        }
        DestroyTask();
    }

    public void DestroyTask()
    {
        Notification.instance.PrintInfo("奖励已获取，或者任务被取消！！");
        TaskManager.instance.tasks.Remove(this);
        TaskManager.instance.getevent -= this.Get;
        TaskManager.instance.checkEvent -= this.Check;
        TaskManager.instance.rewardEvent -= this.Reward;
        Destroy(this);
    }

}
